<?php

class DB_Interface {

    private $conexion;
    private $resource;
    public $sql;
    public static $queries;
    public $error;

    protected function __construct($servidor, $user, $password, $dbname) {

        $this->conexion = mysql_connect($servidor, $user, $password);

        if ($this->conexion === false) {
            error_log("Could not connect to database " . $servidor . ", Check your username and password then try again.\n" . mysql_error());
            die("Error 1, por favor contacte al administrador del sitio.");
        }
        if (!mysql_select_db($dbname, $this->conexion)) {
            error_log("Could not select database" . mysql_error());
#        error_log("Could not select database".mysql_error());
            die("Error 2, por favor contacte al administrador del sitio");
        }
        $this->queries = 0;
        $this->resource = null;
    }

    public function execute() {
        if (!( $this->resource = mysql_query($this->sql, $this->conexion) )) {
            error_log("Error executando query:" . $this->sql . "<br>" . mysql_error());
            die("Error 3, por favor contacte al administrador del sitio");
            return null;
        }
        $this->queries++;
        return $this->resource;
    }

    public function alter($sql) {
        if (!( $this->resource = mysql_query($sql, $this->conexion) )) {
            return false;
        }
        return true;
    }

    public function ultimoId() {
        return mysql_insert_id($this->conexion);
    }

    public function loadObjectList() {
        if (!( $cur = $this->execute() )) {
            return null;
        }
        $array = array();
        while ($row = @mysql_fetch_array($cur)) {
            $array[] = $row;
        }
        @mysql_free_result($cur);
        return $array;
    }

    public function freeResults() {
        @mysql_free_result($this->resource);
        return true;
    }

    public function loadObject() {
        if ($cur = $this->execute()) {
            if ($object = mysql_fetch_object($cur)) {
                @mysql_free_result($cur);
                return $object;
            } else {
                return null;
            }
        } else {
            return false;
        }
    }

    public function fetchAssoc() {

        if ($cur = $this->execute()) {
            if ($data = mysql_fetch_assoc($cur)) {

                return $data;
            } else {
                return null;
            }
        } else {
            return false;
        }
    }

    public function numRows() {

        if ($cur = $this->execute()) {
            if ($data = mysql_num_rows($cur)) {
                @mysql_free_result($cur);
                return $data;
            } else {
                return null;
            }
        } else {
            return false;
        }
    }

    function __destruct() {
        @mysql_free_result($this->resource);
        @mysql_close($this->conexion);
    }

    function getLink() {

        $this->link = mysql_connect("localhost", "bodega", "bodega2014") or die('Could not connect to server.');
        mysql_select_db("BD_GESTOR_INVENTARIOS", $this->link) or die('Could not select database.');

        return $this->link;
    }
}

class DB extends DB_Interface {

    private static $_singleton;

    protected function __construct() {
        parent::__construct('localhost', 'bodega', 'bodega2014', 'bd_gestor_inventarios');
    }

    public static function getInstance() {

        if (is_null(self::$_singleton)) {
            self::$_singleton = new DB();
        }
        return self::$_singleton;
    }
}
?>